var AjaxIMEAssociativeWord = GownFullModule.extend({
	ime: null,
	current_list: null,
	current_query: null,
	last_list: null,
	last_query: null,
	target_list: 'ShiftNum',
	constructor: function()
	{
		var y = this;

		this._ajaxime_assoc_word_functional = function(x) {
			if(x instanceof Function) {
				var dxchg = x();

				if(dxchg && !dxchg.htmlp) y.GenerateAssocWord(dxchg.value);
				return x;
			}
			else return x;
		}
		this._keyboard_observer = function(obj,type,key,modifiers,b)
		{
			if(!b) return false;
			if(type == Const.KEY_TYPE_PRESS) y.StopAssocWord();
			return b;
		};
	},
	module_name: function()
	{
		return 'AjaxIMEAssociativeWord';
	},
	init_module: function(ime,modman)
	{
		var b;

		this.ime = ime; // store reference to IME for further use.
		
		// register a assocword functional
		b = GFFunctionalInterpreter.RegisterFunctional('assocword',this._ajaxime_assoc_word_functional);
		if(!b) {
			Debug.present && Debug.trace(0,'AjaxIMEAssociativeWord: Error, functional :assocword is already registered.');
			return false;
		}
		this.ime.AddKeyboardObserver(this._keyboard_observer);
		return true;
	},
	cleanup_module: function()
	{
		this.ime.RemoveKeyboardObserver(this._keyboard_observer);
		// unregister GoogleSuggest functional
		GFFunctionalInterpreter.UnregisterFunctional('assocword');
		// and cancel all loading suggest..
		this.StopAssocWord();
		return true;
	},
	GenerateAssocWord: function(str)
	{
		var url;

		this.StopAssocWord();
		url = 'http://www.ajaxime.com/alpha2/backend/assoc_word.php?q=' + encodeURI(str);
		// FIXME: Use AjaxIMEAssociativeWord_Callback is somewhat better
		DownloadManager.NewDownloadName('AjaxIMEAssociativeWord',url,function(userdata,serverdata) { userdata.AssocWordCallback(serverdata); },this,true);
	},
	AssocWordCallback: function(ret)
	{
		var x = this;
		var candlist;

		if(!ret.list || ret.list.length == 0) {
			this.ime.candwin.ReleaseList(this,this.target_list);
			this.ime.candwin.UpdateCandidate();
		}
		else {
			if(ret.query instanceof Array) this.current_query = lib.utf16str(ret.query);
			else this.current_query = ret.query;

			if(ret.list[0] instanceof Array) {
				this.current_list = new Array();
				for(var i=0;i<ret.list.length;i++) {
					this.current_list[i] = lib.utf16str(ret.list[i]);
				}
			}
			else {
				this.current_list = ret.list;
			}
			Debug.present && Debug.trace(1,'AjaxIMEAssociativeWord: data=(%s)',ret.list);
			candlist = this.ime.candwin.OccupyList(this,this.target_list);
			if(candlist) {
				candlist.SetList(null,this.current_list,function(obj,pos) { return x.AssocWordHandler(obj,pos); });
				this.ime.candwin.UpdateCandidate();
			}
			else {
				Debug.present && Debug.trace(1,'AjaxIMEAssociativeWord: %s is occupied.',this.target_list);
			}
		}	
	},
	AssocWordHandler: function(obj,pos)
	{
		var str;

		str = this.current_list[pos];
		str = str.replace(new RegExp('^' + this.current_query),'');
		this.ime._SendString(obj,str,true);
		return true;
	},
	StopAssocWord: function()
	{
		DownloadManager.CancelDownloadName('AjaxIMEAssociativeWord');
		this.ime.candwin.ReleaseList(this,this.target_list);
		this.ime.candwin.UpdateCandidate();
	}
});

